Rest 接口风格
默认会暴露存储库的可用链接,SpringData 开箱即用,参考教程。
One of the constraints is to limit the number of verbs. For REST, the primary ones are GET, POST, PUT, DELETE, and PATCH. There are others, but we will not get into them here.
REST 约定了不同 http 请求动作的含义,主要有 GET, POST, PUT, DELETE, PATCH.
-
GET: Fetches the state of a resource without altering the system
-
GET: 仅获取资源状态,不会改变系统
-
POST: Creates a new resource without saying where
-
POST: 创建新的数据资源
-
PUT: Replaces an existing resource, overwriting whatever else (if anything) is already there
-
PUT: 替换现有数据资源
-
DELETE: Removes an existing resource
-
DELETE: 删除现有数据资源
-
PATCH: Alters an existing resource (partially rather than creating a new resource)
-
PATCH: 部分修改现有数据资源
默认返回数据
{
"_links": {
//可用链接
"employees": {
//可用存储库(对应数据库表),支持分页、排序
"href": "http://localhost:8081/api/employees{?page,size,sort}",
"templated": true
},
"students": {
//可用存储库
"href": "http://localhost:8081/api/students{?page,size,sort}",
"templated": true
},
"profile": {
//对象属性(数据库表字段)信息
"href": "http://localhost:8081/api/profile"
}
}
}
存储库返回数据(可以支持分页)
访问 http://localhost:8081/api/employees?page=1&size=1
{
"_embedded": {
"employees": [
//数据列表
{
"firstName": "Frodo",
"lastName": "Baggins",
"description": "ring bearer",
"_links": {
"self": {
"href": "http://localhost:8081/api/employees/2"
},
"employee": {
"href": "http://localhost:8081/api/employees/2"
}
}
}
]
},
"_links": {
"first": {
//第一页链接
"href": "http://localhost:8081/api/employees?page=0&size=1"
},
"prev": {
//上一页链接(如果有上一页的话)
"href": "http://localhost:8081/api/employees?page=0&size=1"
},
"self": {
//自身链接
"href": "http://localhost:8081/api/employees?page=1&size=1"
},
"next": {
//下一页链接(如果有下一页的话)
"href": "http://localhost:8081/api/employees?page=2&size=1"
},
"last": {
//最后一页链接
"href": "http://localhost:8081/api/employees?page=2&size=1"
},
"profile": {
//属性
"href": "http://localhost:8081/api/profile/employees"
}
},
"page": {
//分页信息
"size": 1,
"totalElements": 3,
"totalPages": 3,
"number": 1
}
}
获取存储库字段属性
访问 http://localhost:8081/api/profile/employees ,请求头的 Content-Type 必须为 application/schema+json
{
"title": "Employee",
"properties": {
//字段描述
"firstName": {
"title": "First name",
"readOnly": false,
"type": "string"
},
"lastName": {
"title": "Last name",
"readOnly": false,
"type": "string"
},
"description": {
"title": "Description",
"readOnly": false,
"type": "string"
}
},
"definitions": {},
"type": "object",
"$schema": "http://json-schema.org/draft-04/schema#"
}